package com.yy.bootdemo.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.bootdemo.admin.dao.*;
import com.yy.bootdemo.admin.dto.MenDto;
import com.yy.bootdemo.admin.dto.RoleDto;
import com.yy.bootdemo.admin.entity.MyMenu;
import com.yy.bootdemo.admin.entity.MyRole;
import com.yy.bootdemo.admin.entity.MyRoleMenu;
import com.yy.bootdemo.admin.entity.MyRoleUser;
import com.yy.bootdemo.admin.service.RoleService;
import com.yy.bootdemo.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author codermy
 * @createTime 2020/7/10
 */
@Service
public class RoleServiceImpl  extends ServiceImpl<RoleDao, MyRole> implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RoleMenuDao roleMenuDao;
    @Autowired
    private RoleUserDao roleUserDao;
    @Autowired
    private RoleDeptDao roleDeptDao;
    @Autowired
    private MenuDao menuDao;

    @Override
    public MyRole getRoleById(Integer id) {
        return roleDao.getRoleById(id);
    }

    //根据父节点获取全部子节点
    public static List<MenDto> getChildren(Integer pid, List<MenDto> nodeList){
        List<MenDto> resultList = new ArrayList<>();
        if (null != nodeList && nodeList.size() > 0){
            try{
                for (MenDto layer : nodeList) {
                    if(pid.equals(layer.getMenuId())){
                        //添加子级节点
                        resultList.add(layer);
                        //递归获取深层节点
                        resultList.addAll(getChildren(layer.getParentId(),nodeList));
                    }
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }
        return resultList;
    }
    public static List<Integer> getMenDto(List<Integer> pids, List<MenDto> nodeList){
        List<MenDto> resultList = new ArrayList<>();
        for (Integer pid:pids) {
            List<MenDto> li=  getChildren( pid,  nodeList);
            resultList.addAll(li);
        }
        List<Integer> pIds1=resultList.stream().map(MenDto::getMenuId).collect(Collectors.toList());
        List<Integer> pIds2=pIds1.stream().distinct().collect(Collectors.toList());

        return  pIds2;
    }
    @Override
    public Result update(RoleDto roleDto) {
        List<Integer> menuIds = roleDto.getMenuIds();
        menuIds.remove(0L);
        //1、更新角色权限之前要删除该角色之前的所有权限
        roleMenuDao.deleteRoleMenu(roleDto.getRoleId());
        //2、判断该角色是否有赋予权限值，有就添加"
        if (!CollectionUtils.isEmpty(menuIds)) {
            List<MenDto> list = menuDao.buildAll();
            List<MyMenu> li= menuDao.selectBatchIds(menuIds);
            List<Integer> pIds1=li.stream().map(MyMenu::getParentId).collect(Collectors.toList());
            List<Integer> pIds2=pIds1.stream().distinct().collect(Collectors.toList());
             List<Integer> mpIds2=getMenDto(pIds2,list);
            menuIds.addAll(mpIds2);/**/
            List<Integer> menuIds2=menuIds.stream().distinct().collect(Collectors.toList());
            for (Integer menuId: menuIds2) {
                MyRoleMenu rm=new MyRoleMenu();
                rm.setRoleId(roleDto.getRoleId());
                rm.setMenuId(menuId);
                roleMenuDao.insert(rm);
            }

        }
        //3、更新角色表

        MyRole myRole=new MyRole();
        myRole.setRoleId(roleDto.getRoleId());
        myRole.setRoleName(roleDto.getRoleName());
        myRole.setDescription(roleDto.getDescription());
        myRole.setDataScope(roleDto.getDataScope());
        int countData = roleDao.updateById(roleDto);
        if(countData > 0){
            return Result.ok().message("更新成功");
        }else{
            return Result.error().message("更新失败");
        }
    }


    @Override
    public Result save(RoleDto roleDto) {
        roleDto.setDataScope("1");
        //1、先保存角色"
        roleDao.saveRole(roleDto);
        List<Integer> menuIds = roleDto.getMenuIds();
        //移除0,permission id是从1开始
        //2、保存角色对应的所有权限
        if (!CollectionUtils.isEmpty(menuIds)) {
            roleMenuDao.save(roleDto.getRoleId(), menuIds);
        }
        return Result.ok().message("插入成功");
    }

    @Override
    public Result<MyRole> delete(Integer id) {
        List<MyRoleUser> tbRoleUsers = roleUserDao.listAllRoleUserByRoleId(id);
        if(tbRoleUsers.size() <= 0){
            roleMenuDao.deleteRoleMenu(id);
            roleDao.delete(id);
            roleDeptDao.deleteRoleDept(id);
            return Result.ok().message("删除成功");
        }
        return Result.error().message("该角色已经关联,无法删除");
    }

   }
